<?php
/***************************************************************************
 *				  admin_ug_auth.php
 *				  -------------------
 *	 begin		  : Saturday, Feb 13, 2001
 *	 copyright		  : (C) 2001 The phpBB Group
 *	 email		  : support@phpbb.com
 *
 ***************************************************************************/
/***************************************************************************
 *
 *	 This program is free software; you can redistribute it and/or modify
 *	 it under the terms of the GNU General Public License as published by
 *	 the Free Software Foundation; either version 2 of the License, or
 *	 (at your option) any later version.
 *
 ***************************************************************************/
/* Modifications made by CPG Dev Team http://cpgnuke.com		*/
/************************************************************************/
if (!defined('ADMIN_PAGES')) { exit; }
if (!($count = $db->sql_count(FORUMS_TABLE))) {
	message_die(GENERAL_MESSAGE, 'You need to create a forum before you can set permissions.');
	return;
}
$params = array('mode' => 'mode', 'user_id' => POST_USERS_URL, 'group_id' => POST_GROUPS_URL, 'adv' => 'adv');
while (list($var, $param) = each($params)) {
	if (!empty($_POST[$param]) || !empty($_GET[$param])) {
		$$var = (!empty($_POST[$param])) ? $_POST[$param] : $_GET[$param];
	} else {
		$$var = '';
	}
}

$user_id = intval($user_id);
$group_id = intval($group_id);
$adv = intval($adv);
$mode = htmlprepare($mode);

//
// Start program - define vars
//
$auth_field_match = array(
	'auth_view' => AUTH_VIEW,
	'auth_read' => AUTH_READ,
	'auth_post' => AUTH_POST,
	'auth_reply' => AUTH_REPLY,
	'auth_edit' => AUTH_EDIT,
	'auth_delete' => AUTH_DELETE,
	'auth_sticky' => AUTH_STICKY,
	'auth_announce' => AUTH_ANNOUNCE,
	'auth_vote' => AUTH_VOTE,
	'auth_pollcreate' => AUTH_POLLCREATE,
	'auth_attachments' => AUTH_ATTACH,
	'auth_download' => AUTH_DOWNLOAD);

$field_names = array(
	'auth_view' => $lang['View'],
	'auth_read' => $lang['Read'],
	'auth_post' => $lang['Post'],
	'auth_reply' => $lang['Reply'],
	'auth_edit' => $lang['Edit'],
	'auth_delete' => $lang['Delete'],
	'auth_sticky' => $lang['Sticky'],
	'auth_announce' => $lang['Announce'],
	'auth_vote' => $lang['Vote'],
	'auth_pollcreate' => $lang['Pollcreate'],
	'auth_attachments' => $lang['Auth_attach'],
	'auth_download' => $lang['Auth_download']);

$forum_auth_fields = array_keys($field_names);

// ---------------
// Start Functions
//
function check_auth($type, $key, $u_access, $is_admin) {
	$auth_user = 0;
	if (count($u_access)) {
		for($j = 0; $j < count($u_access); $j++) {
			$result = 0;
			switch($type) {
				case AUTH_ACL:
					$result = $u_access[$j][$key];
				case AUTH_MOD:
					$result = $result || $u_access[$j]['auth_mod'];
				case AUTH_ADMIN:
					$result = $result || $is_admin;
					break;
			}
			$auth_user = $auth_user || $result;
		}
	} else {
		$auth_user = $is_admin;
	}
	return $auth_user;
}
//
// End Functions
// -------------

if (isset($_POST['submit']) && (($mode == 'user' && $user_id) || ($mode == 'group' && $group_id))) {
	$user_level = '';
	if ($mode == 'user') {
		//
		// Get group_id for this user_id
		//
		$result = $db->uFetchAssoc("SELECT g.group_id, u.user_level
			FROM " . USER_GROUP_TABLE . " ug, " . USERS_TABLE . " u, " . GROUPS_TABLE . " g
			WHERE u.user_id = $user_id
			  AND ug.user_id = u.user_id
			  AND g.group_id = ug.group_id
			  AND g.group_single_user = 1");
		$group_id = $row['group_id'];
		$user_level = $row['user_level'];
		if ($group_id < 1) {
			$db->query("INSERT INTO ".GROUPS_TABLE." (group_name, group_description, group_single_user, group_moderator) VALUES ('', 'Personal User', 1, 0)");
			$group_id = $db->insert_id('group_id');
			$db->query("INSERT INTO ".USER_GROUP_TABLE." (user_id, group_id, user_pending) VALUES ($user_id, $group_id, 0)");
			list($user_level) = $db->uFetchRow("SELECT user_level FROM ".USERS_TABLE." WHERE user_id = $user_id");
		}
	}
	//
	// Carry out requests
	//
	if ($mode == 'user' && $_POST['userlevel'] == 'admin' && $user_level != ADMIN) {
		//
		// Make user an admin
		//
		if ($userdata['user_id'] != $user_id) {
			$db->query("UPDATE ".USERS_TABLE." SET user_level = ".ADMIN." WHERE user_id = $user_id");
			$db->query("DELETE FROM ".AUTH_ACCESS_TABLE." WHERE group_id = '$group_id' AND auth_mod = 0");
			//
			// Delete any entries in auth_access, they are not required if user is becoming an admin
			//
			$db->query("UPDATE " . AUTH_ACCESS_TABLE . "
				SET auth_view = 0, auth_read = 0, auth_post = 0, auth_reply = 0, auth_edit = 0, auth_delete = 0, auth_sticky = 0, auth_announce = 0
				WHERE group_id = '$group_id'");
		}
		$message = $lang['Auth_updated'] . '<br /><br />' . sprintf($lang['Click_return_userauth'], '<a href="'.URL::admin('&amp;do=ug_auth&amp;mode='.$mode).'">', '</a>') . '<br /><br />' . sprintf($lang['Click_return_admin_index'], '<a href="'.URL::admin($op).'">', '</a>');
	} else {
		if ($mode == 'user' && $_POST['userlevel'] == 'user' && $user_level == ADMIN) {
			//
			// Make admin a user (if already admin) ... ignore if you're trying
			// to change yourself from an admin to user!
			//
			if ($userdata['user_id'] != $user_id) {
				$db->query("UPDATE ".AUTH_ACCESS_TABLE."
					SET auth_view = 0, auth_read = 0, auth_post = 0, auth_reply = 0, auth_edit = 0, auth_delete = 0, auth_sticky = 0, auth_announce = 0
					WHERE group_id = '$group_id'");
				//
				// Update users level, reset to USER
				//
				$db->query("UPDATE ".USERS_TABLE." SET user_level = ".USER." WHERE user_id = $user_id");
			}
			$message = $lang['Auth_updated'] . '<br /><br />' . sprintf($lang['Click_return_userauth'], '<a href="'.URL::admin("&amp;do=ug_auth&amp;mode=$mode").'">', '</a>') . '<br /><br />' . sprintf($lang['Click_return_admin_index'], '<a href="'.URL::admin($op).'">', '</a>');
		} else {
			$change_mod_list = (isset($_POST['moderator'])) ? $_POST['moderator'] : false;
			if (empty($adv)) {
				$change_acl_list = (isset($_POST['private'])) ? $_POST['private'] : false;
			} else {
				$change_acl_list = array();
				for ($j = 0; $j < count($forum_auth_fields); $j++) {
					$auth_field = $forum_auth_fields[$j];
					if (isset($_POST['private_'.$auth_field])) {
						foreach ($_POST['private_'.$auth_field] as $forum_id => $value) {
							$change_acl_list[$forum_id][$auth_field] = $value;
						}
					}
				}
			}

			if ($mode == 'user') {
				$result = $db->query("SELECT aa.*
				FROM ".AUTH_ACCESS_TABLE." aa, ".USER_GROUP_TABLE." ug, ".GROUPS_TABLE. " g
				WHERE ug.user_id = $user_id
				  AND g.group_id = ug.group_id
				  AND aa.group_id = ug.group_id
				  AND g.group_single_user = 1");
			} else {
				$result = $db->query("SELECT * FROM ".AUTH_ACCESS_TABLE." WHERE group_id = '$group_id'");
			}
			$auth_access = array();
			while ($row = $result->fetch_assoc()) { $auth_access[$row['forum_id']] = $row; }

			$result = $db->query("SELECT * FROM ".FORUMS_TABLE." ORDER BY forum_order");
			$forum_auth_action = $update_acl_status = $update_mod_status = array();
			while ($forum_access = $result->fetch_assoc()) {
				$forum_id = $forum_access['forum_id'];
				$forum_auth_action[$forum_id] = '';

				if ((isset($auth_access[$forum_id]['auth_mod']) && $change_mod_list[$forum_id]['auth_mod'] != $auth_access[$forum_id]['auth_mod']) ||
					(!isset($auth_access[$forum_id]['auth_mod']) && !empty($change_mod_list[$forum_id]['auth_mod'])))
				{
					$update_mod_status[$forum_id] = $change_mod_list[$forum_id]['auth_mod'];
					if (!$update_mod_status[$forum_id]) {
						$forum_auth_action[$forum_id] = 'delete';
					} else if ( !isset($auth_access[$forum_id]['auth_mod']) ) {
						$forum_auth_action[$forum_id] = 'insert';
					} else {
						$forum_auth_action[$forum_id] = 'update';
					}
				}
				for ($j = 0; $j < count($forum_auth_fields); $j++) {
					$auth_field = $forum_auth_fields[$j];
					if ($forum_access[$auth_field] == AUTH_ACL && isset($change_acl_list[$forum_id][$auth_field])) {
						if ((empty($auth_access[$forum_id]['auth_mod']) &&
							(isset($auth_access[$forum_id][$auth_field]) && $change_acl_list[$forum_id][$auth_field] != $auth_access[$forum_id][$auth_field] ) ||
							(!isset($auth_access[$forum_id][$auth_field]) && !empty($change_acl_list[$forum_id][$auth_field]) ) ) ||
							!empty($update_mod_status[$forum_id]) )
						{
							$update_acl_status[$forum_id][$auth_field] = ( !empty($update_mod_status[$forum_id]) ) ? 0 :  $change_acl_list[$forum_id][$auth_field];
							if ( isset($auth_access[$forum_id][$auth_field]) && empty($update_acl_status[$forum_id][$auth_field]) && $forum_auth_action[$forum_id] != 'insert' && $forum_auth_action[$forum_id] != 'update') {
								$forum_auth_action[$forum_id] = 'delete';
							} else if (!isset($auth_access[$forum_id][$auth_field]) && !( $forum_auth_action[$forum_id] == 'delete' && empty($update_acl_status[$forum_id][$auth_field]))) {
								$forum_auth_action[$forum_id] = 'insert';
							} else if ( isset($auth_access[$forum_id][$auth_field]) && !empty($update_acl_status[$forum_id][$auth_field])) {
								$forum_auth_action[$forum_id] = 'update';
							}
						} else if ((empty($auth_access[$forum_id]['auth_mod']) && (isset($auth_access[$forum_id][$auth_field]) && $change_acl_list[$forum_id][$auth_field] == $auth_access[$forum_id][$auth_field])) &&
								   $forum_auth_action[$forum_id] == 'delete')
						{
							$forum_auth_action[$forum_id] = 'update';
						}
					}
				}
			}

			//
			// Checks complete, make updates to DB
			//
			$delete_sql = '';
			foreach ($forum_auth_action as $forum_id => $action) {
				if ($action == 'delete') {
					$delete_sql .= (($delete_sql != '') ? ', ' : '') . $forum_id;
				} elseif ($action == 'insert') {
					$sql_field = '';
					$sql_value = '';
					if (isset($update_acl_status[$forum_id])) {
						foreach ($update_acl_status[$forum_id] as $auth_type => $value) {
//						while (list($auth_type, $value) = each($update_acl_status[$forum_id])) {
							$sql_field .= ( ( $sql_field != '' ) ? ', ' : '' ) . $auth_type;
							$sql_value .= ( ( $sql_value != '' ) ? ', ' : '' ) . $value;
						}
					}
					$sql_field .= ( ( $sql_field != '' ) ? ', ' : '' ) . 'auth_mod';
					$sql_value .= ( ( $sql_value != '' ) ? ', ' : '' ) . ( ( !isset($update_mod_status[$forum_id]) ) ? 0 : $update_mod_status[$forum_id]);
					$db->query("INSERT INTO " . AUTH_ACCESS_TABLE . " (forum_id, group_id, $sql_field)
							VALUES ($forum_id, $group_id, $sql_value)");
				} elseif ($action == 'update') {
					$sql_values = '';
					foreach ($update_acl_status[$forum_id] as $auth_type => $value) {
//					while ( list($auth_type, $value) = each($update_acl_status[$forum_id]) ) {
						$sql_values .= (($sql_values != '') ? ', ' : '') . $auth_type . ' = ' . $value;
					}
					$sql_values .= (($sql_values != '') ? ', ' : '') . 'auth_mod = ' . ((!isset($update_mod_status[$forum_id])) ? 0 : $update_mod_status[$forum_id]);
					$db->query("UPDATE " . AUTH_ACCESS_TABLE . " SET $sql_values
							WHERE group_id = '$group_id' AND forum_id = $forum_id");
				}
			}
			if ($delete_sql != '') {
				$db->query("DELETE FROM " . AUTH_ACCESS_TABLE . " WHERE group_id = '$group_id' AND forum_id IN ($delete_sql)");
			}
			$l_auth_return = ( $mode == 'user' ) ? $lang['Click_return_userauth'] : $lang['Click_return_groupauth'];
			$message = $lang['Auth_updated'] . '<br /><br />' . sprintf($l_auth_return, '<a href="'.URL::admin('&amp;do=ug_auth&amp;mode='.$mode).'">', '</a>') . '<br /><br />' . sprintf($lang['Click_return_admin_index'], '<a href="'.URL::admin($op).'">', '</a>');
		}

		$set_mod = $unset_mod = array();
		//
		// Update user level to mod for appropriate users
		//
		$result = $db->query("SELECT u.user_id
			FROM " . AUTH_ACCESS_TABLE . " aa, " . USER_GROUP_TABLE . " ug, " . USERS_TABLE . " u
			WHERE ug.group_id = aa.group_id
			  AND u.user_id = ug.user_id
			  AND u.user_level NOT IN (".MOD.", ".ADMIN.")
			GROUP BY u.user_id
			HAVING SUM(aa.auth_mod) > 0");
		while ($row = $result->fetch_row()) { $set_mod[] = $row[0]; }
		$result->free();

		//
		// Update user level to user for appropriate users
		//
		switch ( SQL_LAYER )
		{
			case 'postgresql':
				$sql = "SELECT u.user_id
					FROM " . USERS_TABLE . " u, " . USER_GROUP_TABLE . " ug, " . AUTH_ACCESS_TABLE . " aa
					WHERE ug.user_id = u.user_id
					  AND aa.group_id = ug.group_id
					  AND u.user_level NOT IN (" . USER . ", " . ADMIN . ")
					GROUP BY u.user_id
					HAVING SUM(aa.auth_mod) = 0
					UNION (
						SELECT u.user_id
						FROM " . USERS_TABLE . " u
						WHERE NOT EXISTS (
							SELECT aa.auth_mod
							FROM " . USER_GROUP_TABLE . " ug, " . AUTH_ACCESS_TABLE . " aa
							WHERE ug.user_id = u.user_id
							  AND aa.group_id = ug.group_id
						)
						AND u.user_level NOT IN (" . USER . ", " . ADMIN . ")
						GROUP BY u.user_id
					)";
				break;
			case 'oracle':
				$sql = "SELECT u.user_id
					FROM " . USERS_TABLE . " u, " . USER_GROUP_TABLE . " ug, " . AUTH_ACCESS_TABLE . " aa
					WHERE ug.user_id = u.user_id(+)
					  AND aa.group_id = ug.group_id(+)
					  AND u.user_level NOT IN (" . USER . ", " . ADMIN . ")
					GROUP BY u.user_id
					HAVING SUM(aa.auth_mod) = 0";
				break;
			default:
				$sql = "SELECT u.user_id
					FROM " . USERS_TABLE . " u
					LEFT JOIN " . USER_GROUP_TABLE . " ug ON ug.user_id = u.user_id
					LEFT JOIN " . AUTH_ACCESS_TABLE . " aa ON aa.group_id = ug.group_id
					WHERE u.user_level NOT IN (" . USER . ", " . ADMIN . ")
					GROUP BY u.user_id
					HAVING SUM(aa.auth_mod) = 0";
				break;
		}
		$result = $db->query($sql);
		while ($row = $result->fetch_row()) { $unset_mod[] = $row[0]; }
		$result->free();

		if (!empty($set_mod)) {
//			die(print_r($set_mod));
			$db->query("UPDATE ".USERS_TABLE." SET user_level = ".MOD.' WHERE user_id IN ('.implode(',', $set_mod).')');
		}
		if (!empty($unset_mod)) {
//			die(print_r($unset_mod));
			$db->query("UPDATE ".USERS_TABLE." SET user_level = ".USER.' WHERE user_id IN ('.implode(',', $unset_mod).')');
		}
	}
	message_die(GENERAL_MESSAGE, $message);
	BoardCache::cacheDelete('moderators');
}
else if (($mode == 'user' && (isset($_POST['username']) || $user_id ) ) || ($mode == 'group' && $group_id))
{
	if (isset($_POST['username'])) {
		$this_userdata = getusrdata($_POST['username']);
		if (!is_array($this_userdata)) {
			message_die(GENERAL_MESSAGE, $lang['No_such_user']);
			return;
		}
		$user_id = $this_userdata['user_id'];
	}
	//
	// Front end
	//
	$result = $db->query("SELECT * FROM " . FORUMS_TABLE . " ORDER BY forum_order");
	$forum_access = array();
	while ($row = $result->fetch_assoc()) {
		$forum_access[] = $row;
	}
	$result->free();
	if (empty($adv)) {
		for($i = 0; $i < count($forum_access); $i++) {
			$forum_id = $forum_access[$i]['forum_id'];
			$forum_auth_level[$forum_id] = AUTH_ALL;
			for($j = 0; $j < count($forum_auth_fields); $j++) {
				$forum_access[$i][$forum_auth_fields[$j]] . ' :: ';
				if ( $forum_access[$i][$forum_auth_fields[$j]] == AUTH_ACL ) {
					$forum_auth_level[$forum_id] = AUTH_ACL;
					$forum_auth_level_fields[$forum_id][] = $forum_auth_fields[$j];
				}
			}
		}
	}
	//
	// Check if a private user group exists for this user and if not, create one.
	//
	if ($mode == 'user') {
		$row = $db->uFetchRow("SELECT user_id FROM " . USER_GROUP_TABLE . " WHERE user_id = $user_id");
		if (!$row || $row[0] != $user_id) {
			if (!($row = $db->uFetchRow("SELECT MAX(group_id) FROM ".GROUPS_TABLE))) {
				message_die(GENERAL_ERROR, 'Could not obtain next group_id information', '', __LINE__, __FILE__, $sql);
				return;
			}
			$group_id = $row[0] + 1;
			$db->query("INSERT INTO ".GROUPS_TABLE." (group_id, group_name, group_description, group_single_user, group_moderator)
				VALUES ($group_id, '', 'Personal User', 1, 0)");
			$db->query("INSERT INTO " . USER_GROUP_TABLE . " (group_id, user_id, user_pending) VALUES ($group_id, $user_id, 0)");
		}
	}
	//
	//	End Private group check.
	//
	$sql = "SELECT u.user_id, u.username, u.user_level, g.group_id, g.group_name, g.group_single_user FROM " . USERS_TABLE . " u, " . GROUPS_TABLE . " g, " . USER_GROUP_TABLE . " ug WHERE ";
	$sql .= ($mode == 'user') ? "u.user_id = $user_id AND ug.user_id = u.user_id AND g.group_id = ug.group_id" : "g.group_id = $group_id AND ug.group_id = g.group_id AND u.user_id = ug.user_id AND ug.user_pending = 0";
	$result = $db->query($sql);
	$ug_info = array();
	while ($row = $result->fetch_assoc()) {
		$ug_info[] = $row;
	}
	$result->free();

	if ($mode == 'user') {
		$result = $db->query("SELECT aa.*, g.group_single_user
			FROM ".AUTH_ACCESS_TABLE." aa, ".USER_GROUP_TABLE." ug, ".GROUPS_TABLE." g
			WHERE ug.user_id = $user_id
			  AND g.group_id = ug.group_id
			  AND aa.group_id = ug.group_id
			  AND g.group_single_user = 1");
	} else {
		$result = $db->query("SELECT * FROM ".AUTH_ACCESS_TABLE." WHERE group_id = '$group_id'");
	}
	$auth_access = $auth_access_count = array();
	while ($row = $result->fetch_assoc()) {
		$auth_access[$row['forum_id']][] = $row;
		if (!isset($auth_access_count[$row['forum_id']])) $auth_access_count[$row['forum_id']] = 0;
		$auth_access_count[$row['forum_id']]++;
	}
	$result->free();

	$is_admin = ($mode == 'user') ? (($ug_info[0]['user_level'] == ADMIN && $ug_info[0]['user_id'] != ANONYMOUS) ? 1 : 0) : 0;

	$auth_ug = array();
	for ($i = 0; $i < count($forum_access); $i++) {
		$forum_id = $forum_access[$i]['forum_id'];
		unset($prev_acl_setting);
		for ($j = 0; $j < count($forum_auth_fields); $j++) {
			$key = $forum_auth_fields[$j];
			$value = $forum_access[$i][$key];

			switch( $value )
			{
				case AUTH_ALL:
				case AUTH_REG:
					$auth_ug[$forum_id][$key] = 1;
					break;

				case AUTH_ACL:
					$auth_ug[$forum_id][$key] = (!empty($auth_access_count[$forum_id])) ? check_auth(AUTH_ACL, $key, $auth_access[$forum_id], $is_admin) : 0;
					$auth_field_acl[$forum_id][$key] = $auth_ug[$forum_id][$key];
					if (isset($prev_acl_setting)) {
						if ( $prev_acl_setting != $auth_ug[$forum_id][$key] && empty($adv) ) {
							$adv = 1;
						}
					}
					$prev_acl_setting = $auth_ug[$forum_id][$key];
					break;

				case AUTH_MOD:
					$auth_ug[$forum_id][$key] = ( !empty($auth_access_count[$forum_id]) ) ? check_auth(AUTH_MOD, $key, $auth_access[$forum_id], $is_admin) : 0;
					break;

				case AUTH_ADMIN:
					$auth_ug[$forum_id][$key] = $is_admin;
					break;

				default:
					$auth_ug[$forum_id][$key] = 0;
					break;
			}
		}
		//
		// Is user a moderator?
		//
		$auth_ug[$forum_id]['auth_mod'] = ( !empty($auth_access_count[$forum_id]) ) ? check_auth(AUTH_MOD, 'auth_mod', $auth_access[$forum_id], 0) : 0;
	}

	$i = 0;
	foreach ($auth_ug as $forum_id => $user_ary) {
		if (empty($adv)) {
			if ($forum_auth_level[$forum_id] == AUTH_ACL) {
				$allowed = 1;
				for($j = 0; $j < count($forum_auth_level_fields[$forum_id]); $j++) {
					if (!$auth_ug[$forum_id][$forum_auth_level_fields[$forum_id][$j]]) {
						$allowed = 0;
					}
				}
				$optionlist_acl = '<select name="private[' . $forum_id . ']">';
				if ( $is_admin || $user_ary['auth_mod'] ) {
					$optionlist_acl .= '<option value="1">' . $lang['Allowed_Access'] . '</option>';
				} else if ( $allowed ) {
					$optionlist_acl .= '<option value="1" selected="selected">' . $lang['Allowed_Access'] . '</option><option value="0">'. $lang['Disallowed_Access'] . '</option>';
				} else {
					$optionlist_acl .= '<option value="1">' . $lang['Allowed_Access'] . '</option><option value="0" selected="selected">' . $lang['Disallowed_Access'] . '</option>';
				}
				$optionlist_acl .= '</select>';
			} else {
				$optionlist_acl = '&nbsp;';
			}
		} else {
			$optionlist_acl_adv = array();
			for ($j = 0; $j < count($forum_access); $j++) {
				if ($forum_access[$j]['forum_id'] == $forum_id) {
					for ($k = 0; $k < count($forum_auth_fields); $k++) {
						$field_name = $forum_auth_fields[$k];
						if ($forum_access[$j][$field_name] == AUTH_ACL) {
							$optionlist_acl_adv[$forum_id][$k] = '<select name="private_' . $field_name . '[' . $forum_id . ']">';
							if (isset($auth_field_acl[$forum_id][$field_name]) && !($is_admin || $user_ary['auth_mod']) ) {
								if( !$auth_field_acl[$forum_id][$field_name] ) {
									$optionlist_acl_adv[$forum_id][$k] .= '<option value="1">' . $lang['ON'] . '</option><option value="0" selected="selected">' . $lang['OFF'] . '</option>';
								} else {
									$optionlist_acl_adv[$forum_id][$k] .= '<option value="1" selected="selected">' . $lang['ON'] . '</option><option value="0">' . $lang['OFF'] . '</option>';
								}
							} else {
								if( $is_admin || $user_ary['auth_mod'] ) {
									$optionlist_acl_adv[$forum_id][$k] .= '<option value="1">' . $lang['ON'] . '</option>';
								} else {
									$optionlist_acl_adv[$forum_id][$k] .= '<option value="1">' . $lang['ON'] . '</option><option value="0" selected="selected">' . $lang['OFF'] . '</option>';
								}
							}
							$optionlist_acl_adv[$forum_id][$k] .= '</select>';
						} else {
							$optionlist_acl_adv[$forum_id][$k] = '';
						}
					}
				}
			}
		}

		$optionlist_mod = '<select name="moderator[' . $forum_id . ']">';
		$optionlist_mod .= ( $user_ary['auth_mod'] ) ? '<option value="1" selected="selected">' . $lang['Is_Moderator'] . '</option><option value="0">' . $lang['Not_Moderator'] . '</option>' : '<option value="1">' . $lang['Is_Moderator'] . '</option><option value="0" selected="selected">' . $lang['Not_Moderator'] . '</option>';
		$optionlist_mod .= '</select>';

		$template->assign_block_vars('forums', array(
			'FORUM_NAME' => $forum_access[$i]['forum_name'],
			'U_FORUM_AUTH' => URL::admin("&amp;do=forumauth&amp;f=" . $forum_access[$i]['forum_id']),
			'S_MOD_SELECT' => $optionlist_mod)
		);

		if (empty($adv)) {
			$template->assign_block_vars('forums.aclvalues', array(
				'S_ACL_SELECT' => $optionlist_acl)
			);
		} else {
			for($j = 0; $j < count($forum_auth_fields); $j++) {
				$template->assign_block_vars('forums.aclvalues', array(
					'S_ACL_SELECT' => $optionlist_acl_adv[$forum_id][$j])
				);
			}
		}
		$i++;
	}

	if ($mode == 'user') {
		$t_username = $ug_info[0]['username'];
		$s_user_type = ( $is_admin ) ? '<select name="userlevel"><option value="admin" selected="selected">' . $lang['Auth_Admin'] . '</option><option value="user">' . $lang['Auth_User'] . '</option></select>' : '<select name="userlevel"><option value="admin">' . $lang['Auth_Admin'] . '</option><option value="user" selected="selected">' . $lang['Auth_User'] . '</option></select>';
	} else {
		$t_groupname = $ug_info[0]['group_name'];
	}

	$name = $id = array();
	for ($i = 0; $i < count($ug_info); $i++) {
		if (($mode == 'user' && !$ug_info[$i]['group_single_user']) || $mode == 'group') {
			$name[] = ($mode == 'user') ? $ug_info[$i]['group_name'] :	$ug_info[$i]['username'];
			$id[] = ($mode == 'user') ? intval($ug_info[$i]['group_id']) : intval($ug_info[$i]['user_id']);
		}
	}

	if (count($name)) {
		$t_usergroup_list = '';
		for ($i = 0; $i < count($ug_info); $i++) {
			if ($mode == 'user' && $ug_info[$i]['group_single_user']) continue;
			$ug = ($mode == 'user') ? 'group&amp;' . POST_GROUPS_URL : 'user&amp;' . POST_USERS_URL;
			$t_usergroup_list .= (($t_usergroup_list != '') ? ', ' : '').'<a href="'.URL::admin('&amp;do=ug_auth&amp;mode='.$ug.'='.$id[$i]).'">' . $name[$i] . '</a>';
		}
	} else {
		$t_usergroup_list = $lang['None'];
	}

	$s_column_span = 2; // Two columns always present
	if (!$adv) {
		$template->assign_block_vars('acltype', array(
			'L_UG_ACL_TYPE' => $lang['Simple_Permission'])
		);
		$s_column_span++;
	} else {
		for($i = 0; $i < count($forum_auth_fields); $i++) {
			$cell_title = $field_names[$forum_auth_fields[$i]];
			$template->assign_block_vars('acltype', array(
				'L_UG_ACL_TYPE' => $cell_title)
			);
			$s_column_span++;
		}
	}

	$template->set_filenames(array('body' => 'forums/admin/auth_ug_body.html'));

	$adv_switch = ( empty($adv) ) ? 1 : 0;
	$u_ug_switch = ( $mode == 'user' ) ? POST_USERS_URL . "=" . $user_id : POST_GROUPS_URL . "=" . $group_id;
	$switch_mode = URL::admin("&amp;do=ug_auth&amp;mode=$mode&amp;" . $u_ug_switch . "&amp;adv=$adv_switch");
	$switch_mode_text = ( empty($adv) ) ? $lang['Advanced_mode'] : $lang['Simple_mode'];
	$u_switch_mode = '<a href="' . $switch_mode . '">' . $switch_mode_text . '</a>';

	$s_hidden_fields = '<input type="hidden" name="mode" value="' . $mode . '" /><input type="hidden" name="adv" value="' . $adv . '" />';
	$s_hidden_fields .= ( $mode == 'user' ) ? '<input type="hidden" name="' . POST_USERS_URL . '" value="' . $user_id . '" />' : '<input type="hidden" name="' . POST_GROUPS_URL . '" value="' . $group_id . '" />';

	if ($mode == 'user') {
		$template->assign_block_vars('switch_user_auth', array());
		$template->assign_vars(array(
			'USERNAME' => $t_username,
			'USER_LEVEL' => $lang['User_Level'] . " : " . $s_user_type,
			'USER_GROUP_MEMBERSHIPS' => $lang['Group_memberships'] . ' : ' . $t_usergroup_list)
		);
	} else {
		$template->assign_block_vars("switch_group_auth", array());
		$template->assign_vars(array(
			'USERNAME' => $t_groupname,
			'GROUP_MEMBERSHIP' => $lang['Usergroup_members'] . ' : ' . $t_usergroup_list)
		);
	}

	$template->assign_vars(array(
		'L_USER_OR_GROUPNAME' => ( $mode == 'user' ) ? $lang['Username'] : $lang['Group_name'],

		'L_AUTH_TITLE' => ( $mode == 'user' ) ? $lang['Auth_Control_User'] : $lang['Auth_Control_Group'],
		'L_AUTH_EXPLAIN' => ( $mode == 'user' ) ? $lang['User_auth_explain'] : $lang['Group_auth_explain'],
		'L_MODERATOR_STATUS' => $lang['Moderator_status'],
		'L_PERMISSIONS' => $lang['Permissions'],
		'L_SUBMIT' => $lang['Submit'],
		'L_RESET' => $lang['Reset'],
		'L_FORUM' => $lang['Forum'],

		'U_USER_OR_GROUP' => URL::admin("&amp;do=ug_auth"),
		'U_SWITCH_MODE' => $u_switch_mode,

		'S_COLUMN_SPAN' => $s_column_span,
		'S_AUTH_ACTION' => URL::admin("&amp;do=ug_auth"),
		'S_HIDDEN_FIELDS' => $s_hidden_fields)
	);
} else if ('user' == $mode) {
	//
	// Select a user
	//
	$template->set_filenames(array('body' => 'forums/admin/user_select_body.html'));

	$template->assign_vars(array(
		'L_FIND_USERNAME' => $lang['Find_username'],
		'U_SEARCH_USER' => URL::index("Forums&amp;file=search&amp;mode=searchuser&amp;popup=1&amp;menu=1", true, true))
	);

	$s_hidden_fields = '<input type="hidden" name="mode" value="' . $mode . '" />';

	$template->assign_vars(array(
		'L_USER_TITLE' => $lang['Auth_Control_User'],
		'L_USER_EXPLAIN' => $lang['User_auth_explain'],
		'L_USER_SELECT' => $lang['Select_a_User'],
		'L_LOOK_UP' => $lang['Look_up_User'],

		'S_HIDDEN_FIELDS' => $s_hidden_fields,
		'S_USER_ACTION' => URL::admin("&amp;do=ug_auth")
	));
} else {
	//
	// Select a group
	//
	$template->set_filenames(array('body' => 'admin/forums/ug_auth_select_group'));
	$template->forum_groups = $db->query("SELECT
		group_id id,
		group_name name
	FROM " . GROUPS_TABLE . "
	WHERE group_single_user <> 1
	ORDER BY group_name");
}
